package com.neuedu.corejava.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class OracleJDBC {

	public static void main(String[] args) {
		// 4个参数:
		// 驱动
		String driver = "oracle.jdbc.driver.OracleDriver";
		// URL
		String url = "jdbc:oracle:thin://localhost:1521/orcl";
		// 帐号
		String user = "student";
		// 密码
		String password = "123456";

		// 6个步骤：
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			// 1.注册驱动
			Class.forName(driver);
			// 2.连接数据库- Connection
			conn = DriverManager.getConnection(url, user, password);
			// 3.打开状态连接 - Statement
			stmt = conn.createStatement();
			// 4.执行sql -
			String sql = "select * from  cla";
			rs = stmt.executeQuery(sql);
			// 5.处理sql执行结果
			while (rs.next()) {
				int clano = rs.getInt(1);
				String claid = rs.getString(2);
				String claname = rs.getString(3);
				System.out.println(clano + " ：  " + claid + " ：  " + claname);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 6.关闭连接，释放资源
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
				}
			}
			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException e) {
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
				}
			}
		}

	}

}
